package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Scrapped;
import com.zyd.shiro.business.service.PakScrappedService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakScrapped;
import com.zyd.shiro.persistence.mapper.PakScrappedMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakScrappedServiceImpl implements PakScrappedService {

    @Autowired
    private PakScrappedMapper pakScrappedMapper;

    @Override
    public Scrapped insert(Scrapped scrapped) {
        Assert.notNull(scrapped, "对象不可为空！");
        scrapped.getPakScrapped().setUnicode("BF"+System.currentTimeMillis());
        scrapped.getPakScrapped().setAppstatus(0);
        scrapped.getPakScrapped().setScrstatus(0);
        scrapped.getPakScrapped().setUpdateTime(new Date());
        scrapped.getPakScrapped().setCreateTime(new Date());
        pakScrappedMapper.insertSelective(scrapped.getPakScrapped());
        return scrapped;
    }

    @Override
    public void insertList(List<Scrapped> scrappeds) {
            for(Scrapped scrapped:scrappeds){
               scrapped.getPakScrapped().setUpdateTime(new Date());
               scrapped.getPakScrapped().setCreateTime(new Date());
               pakScrappedMapper.insertSelective(scrapped.getPakScrapped());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakScrappedMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Scrapped scrapped) {
        Assert.notNull(scrapped, "对象不可为空！");
        scrapped.setUpdateTime(new Date());
        return pakScrappedMapper.updateByPrimaryKey(scrapped.getPakScrapped()) > 0;
    }

    @Override
    public boolean updateSelective(Scrapped scrapped) {
        Assert.notNull(scrapped, "对象不可为空！");
        scrapped.setUpdateTime(new Date());
        return pakScrappedMapper.updateByPrimaryKeySelective(scrapped.getPakScrapped()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Scrapped getByPrimaryKey(Long primaryKey) {
        PakScrapped pakScrapped = pakScrappedMapper.selectByPrimaryKey(primaryKey);
        return null == pakScrapped ? null : new Scrapped(pakScrapped);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Scrapped getOneByEntity(Scrapped test) {
        Assert.notNull(test, "对象不可为空！");
        PakScrapped pakScrapped = pakScrappedMapper.selectOne(test.getPakScrapped());
        return null == pakScrapped ? null : new Scrapped(pakScrapped);
    }

    @Override
    public List<Scrapped> listAll() {
        List<PakScrapped> pakScrappeds = pakScrappedMapper.selectAll();
        if (CollectionUtils.isEmpty(pakScrappeds)) {
            return null;
        }
        List<Scrapped> scrappeds = new ArrayList<>();
        for (PakScrapped pakScrapped : pakScrappeds) {
            scrappeds.add(new Scrapped(pakScrapped));
        }
        return scrappeds;
    }

    @Override
    public List<Scrapped> listByEntity(Scrapped entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Scrapped> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakScrapped> pakScrappeds = pakScrappedMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakScrappeds)) {
            return null;
        }
        List<Scrapped> scrappeds = new ArrayList<>();
        for (PakScrapped pakScrapped : pakScrappeds) {
            scrappeds.add(new Scrapped(pakScrapped));
        }
        PageInfo bean = new PageInfo<PakScrapped>(pakScrappeds);
        bean.setList(scrappeds);
        return bean;
    }
@Override
    public void increase(PakScrapped pakScrapped) {
        pakScrappedMapper.insert(pakScrapped);
    }




}
